﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1.SensorTable
{
    public partial class FormHistoryAlarm : Form
    {
        MONITOR_DBDataContext alarm_context = new MONITOR_DBDataContext();

        public FormHistoryAlarm()
        {
            InitializeComponent();
        }

        private void dataGridViewZoneColumnRefresh()
        {
            //为地区列赋值
            for (int i = 0; i < tb_alarmDataGridView.Rows.Count; i++)
            {
                if (tb_alarmDataGridView.Rows[i].Cells[dataGridViewTextBoxColumn2.Name].Value != null)
                {
                    int mon_id = Convert.ToInt32(tb_alarmDataGridView.Rows[i].Cells[dataGridViewTextBoxColumn3.Name].Value.ToString());

                    // 获取当前行监控箱号
                    IEnumerable<tb_monitor_device> qZone =
                    from mon in alarm_context.tb_monitor_device
                    where mon.ID == mon_id
                    select mon;

                    foreach (var item in qZone)
                    {
                        tb_alarmDataGridView.Rows[i].Cells[zone.Name].Value = item.Zone;
                    }
                }
            }
        }

        private void FormHistoryAlarm_Load(object sender, EventArgs e)
        {
            try
            {
                this.tb_alarmBindingSource.DataSource = alarm_context.tb_alarm;

                // 刷新地区列
                dataGridViewZoneColumnRefresh();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //private void tb_alarmBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        tb_alarmDataGridView.EndEdit();
        //        alarm_context.SubmitChanges();

        //        MessageBox.Show("保存成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //    }
        //    catch (Exception Ex)
        //    {
        //        MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        Monitor_DB.instance.Refresh_DB();
        //    }
        //}

        //private void tb_alarmDataGridView_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.Control == true && e.KeyCode == Keys.S)
        //    {
        //        tb_alarmBindingNavigatorSaveItem_Click(sender, e);
        //    }
        //}

        private void tb_alarmDataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            // 刷新地区列
            dataGridViewZoneColumnRefresh();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //alarm_context.Refresh(RefreshMode.OverwriteCurrentValues, alarm_context.tb_alarm);
            alarm_context.Dispose();
            alarm_context = new MONITOR_DBDataContext();

            this.tb_alarmBindingSource.DataSource = alarm_context.tb_alarm;

            this.tb_alarmDataGridView.Refresh();
        }

        private void tb_alarmBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            try
            {
                tb_alarmDataGridView.EndEdit();

                alarm_context.SubmitChanges();

                MessageBox.Show("保存成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
